<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/8">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/50.shell 练习-判断pid是否一致/">shell 练习-判断pid是否一致</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>先普及一小段知识,我们用ps aux可以查看到进程的PID，而每个PID都会在/proc内产生。如果查看到的pid而proc内是没有的，则是进程被人修改了，这就代表你的系统很有可能已经被入侵过了。</p>
<p>请大家用上面知识编写一个shell，定期检查下自己的系统是否被人入侵过。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">ps aux|awk <span class="string">'/[0-9]/ &#123;print $2&#125;'</span>|<span class="keyword">while</span> <span class="built_in">read</span> pid</div><div class="line"><span class="keyword">do</span></div><div class="line">    result=`find /proc/ -maxdepth 1 -<span class="built_in">type</span> d -name <span class="string">"<span class="variable">$pid</span>"</span>`</div><div class="line">    <span class="keyword">if</span> [ -z <span class="variable">$result</span> ]; <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"<span class="variable">$pid</span> abnormal!"</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/49. shell 练习-更改后缀名/">shell 练习-更改后缀名</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<ol>
<li><p>编写一个名为chname的程序，将当前目录下所有的.txt文件更名为.doc文件。</p>
</li>
<li><p>编写一个名为chuser的程序，执行中每隔5分钟检查指定的用户是否登录系统，用户名从命令行输入；如果指定的用户已经登录，则显示相关信息。</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">find . -<span class="built_in">type</span> f -name <span class="string">"*.txt"</span> &gt; /tmp/txt.list</div><div class="line"><span class="keyword">for</span> f <span class="keyword">in</span> `cat /tmp/txt.list`</div><div class="line"><span class="keyword">do</span></div><div class="line">    n=`<span class="built_in">echo</span> <span class="variable">$f</span>|sed -r <span class="string">'s/(.*)\.txt/\1/'</span>`</div><div class="line">    <span class="built_in">echo</span> <span class="string">"mv <span class="variable">$f</span> <span class="variable">$n</span>.doc"</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>或</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input the username: "</span> user</div><div class="line"><span class="keyword">while</span> :</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">if</span> who | grep -qw <span class="variable">$user</span></div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="variable">$user</span> login.</div><div class="line">    <span class="keyword">else</span></div><div class="line">        <span class="built_in">echo</span> <span class="variable">$user</span> not login.</div><div class="line">    <span class="keyword">fi</span></div><div class="line">    sleep 300</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/48. shell 练习-判断用户是否登录中/">shell 练习-判断用户是否登录中</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<ol>
<li><p>编写一个名为ifuser的程序，它执行时带用户名作为命令行参数，判断该用户是否已经在系统中登录，并给出相关信息。</p>
</li>
<li><p>编写一个名为menu的程序，实现简单的弹出式菜单功能，用户能根据显示的菜单项从键盘选择执行对应的命令。</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input the username: "</span> user</div><div class="line"><span class="keyword">if</span> who | grep -qw <span class="variable">$user</span></div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$user</span> is online.</div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$user</span> not online.</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>或者</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">function</span> message()</div><div class="line">&#123;</div><div class="line">    <span class="built_in">echo</span> <span class="string">"0. w"</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"1. ls"</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"2.quit"</span></div><div class="line">    <span class="built_in">read</span> -p <span class="string">"Please input parameter: "</span> Par</div><div class="line">&#125;</div><div class="line">message</div><div class="line"><span class="keyword">while</span> [ <span class="variable">$Par</span> -ne <span class="string">'2'</span> ] ; <span class="keyword">do</span></div><div class="line">    <span class="keyword">case</span> <span class="variable">$Par</span> <span class="keyword">in</span></div><div class="line">    0)</div><div class="line">        w</div><div class="line">        ;;</div><div class="line">    1)</div><div class="line">        ls</div><div class="line">        ;;</div><div class="line">    2)</div><div class="line">        <span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"Unkown command"</span></div><div class="line">        ;;</div><div class="line">  <span class="keyword">esac</span></div><div class="line">  message</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/47. shell 练习-判断某个文件是否存在/">shell 练习-判断某个文件是否存在</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<ol>
<li><p>编写一个名为iffile程序，它执行时判断/bin目录下date文件是否存在？</p>
</li>
<li><p>编写一个名为greet的问候程序，它执行时能根据系统当前的时间向用户输出问候信息。设从半夜到中午为早晨，中午到下午六点为下午，下午六点到半夜为晚上。</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">if</span> [ -f /bin/date  ] </div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"/bin/date file exist."</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"/bin/date not exist."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>或者</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">h=`date +%H`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$h</span> -ge 0 ] &amp;&amp; [ <span class="variable">$h</span> -lt 12 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"Good morning."</span></div><div class="line"><span class="keyword">elif</span> [ <span class="variable">$h</span> -ge 12 ] &amp;&amp; [ <span class="variable">$h</span> -lt 18 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"Good afternoon."</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"Good evening."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/46. shell 练习-格式化输出数字串/">shell 练习-格式化输出数字串</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>输入一串随机数字，然后按千分位输出。</p>
<p>比如输入数字串为“123456789”，输出为123,456,789</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"输入一串数字："</span> num</div><div class="line">v=`<span class="built_in">echo</span> <span class="variable">$num</span>|sed <span class="string">'s/[0-9]//g'</span>`</div><div class="line"><span class="keyword">if</span> [ -n <span class="string">"<span class="variable">$v</span>"</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"请输入纯数字."</span></div><div class="line">    <span class="built_in">exit</span></div><div class="line"><span class="keyword">fi</span></div><div class="line">length=<span class="variable">$&#123;#num&#125;</span></div><div class="line">len=0</div><div class="line">sum=<span class="string">''</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> $(seq 1 <span class="variable">$length</span>)</div><div class="line"><span class="keyword">do</span></div><div class="line">        len=$[<span class="variable">$len</span>+1]</div><div class="line">        <span class="keyword">if</span> [[ <span class="variable">$len</span> == 3 ]]</div><div class="line">        <span class="keyword">then</span></div><div class="line">                sum=<span class="string">','</span><span class="variable">$&#123;num:$[0-$i]:1&#125;</span><span class="variable">$sum</span></div><div class="line">                len=0</div><div class="line">        <span class="keyword">else</span></div><div class="line">                sum=<span class="variable">$&#123;num:$[0-$i]:1&#125;</span><span class="variable">$sum</span></div><div class="line">        <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="keyword">if</span> [[ -n $(<span class="built_in">echo</span> <span class="variable">$sum</span> | grep <span class="string">'^,'</span> ) ]]</div><div class="line"><span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="variable">$&#123;sum:1&#125;</span></div><div class="line"><span class="keyword">else</span></div><div class="line">        <span class="built_in">echo</span> <span class="variable">$sum</span></div><div class="line"><span class="keyword">fi</span></div><div class="line">上面这个答案比较复杂，下面再来一个sed的</div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"输入一串数字："</span> num</div><div class="line">v=`<span class="built_in">echo</span> <span class="variable">$num</span>|sed <span class="string">'s/[0-9]//g'</span>`</div><div class="line"><span class="keyword">if</span> [ -n <span class="string">"<span class="variable">$v</span>"</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"请输入纯数字."</span></div><div class="line">    <span class="built_in">exit</span></div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="built_in">echo</span> <span class="variable">$num</span>|sed -r <span class="string">'&#123;:number;s/([0-9]+)([0-9]&#123;3&#125;)/\1,\2/;t number&#125;'</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/45. shell 练习-检查错误/">shell 练习-检查错误</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个shell脚本，检查指定的shell脚本是否有语法错误，若有错误，首先显示错误信息，然后提示用户输入q或者Q退出脚本，输入其他内容则直接用vim打开该shell脚本。</p>
<p>提醒： 检查shell脚本有没有语法错误的命令是  sh -n   xxx.sh</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">sh -n <span class="variable">$1</span> 2&gt;/tmp/err</div><div class="line"><span class="keyword">if</span> [ $? -eq <span class="string">"0"</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"The script is OK."</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    cat /tmp/err</div><div class="line">    <span class="built_in">read</span> -p <span class="string">"Please inpupt Q/q to exit, or others to edit it by vim. "</span> n</div><div class="line">    <span class="keyword">if</span> [ -z <span class="variable">$n</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        vim <span class="variable">$1</span></div><div class="line">        <span class="built_in">exit</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$n</span> == <span class="string">"q"</span> -o <span class="variable">$n</span> == <span class="string">"Q"</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">exit</span></div><div class="line">    <span class="keyword">else</span></div><div class="line">        vim <span class="variable">$1</span></div><div class="line">        <span class="built_in">exit</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/44. shell 练习-日志归档/">shell 练习-日志归档</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>类似于日志切割，系统有个logrotate程序，可以完成归档。但现在我们要自己写一个shell脚本实现归档。 </p>
<p>举例： 假如服务的输出日志是1.log，我要求每天归档一个，1.log第二天就变成1.log.1，第三天1.log.2, 第四天 1.log.3  一直到1.log.5</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">function</span> e_df()</div><div class="line">&#123;</div><div class="line">    [ -f <span class="variable">$1</span> ] &amp;&amp; rm -f <span class="variable">$1</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 5 -1 2`</div><div class="line"><span class="keyword">do</span></div><div class="line">    i2=$[<span class="variable">$i</span>-1]</div><div class="line">    e_df /data/1.log.<span class="variable">$i</span></div><div class="line">    <span class="keyword">if</span> [ -f /data/1.log.<span class="variable">$i2</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        mv /data/1.log.<span class="variable">$i2</span> /data/1.log.<span class="variable">$i</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div><div class="line"></div><div class="line">e_df /data/1.log.1</div><div class="line">mv /data/1.log  /data/1.log.1</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/43. shell 练习-打印只有一个数字的行/">shell 练习-打印只有一个数字的行</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>如题，把一个文本文档中只有一个数字的行给打印出来。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">f=/etc/passwd</div><div class="line">line=`wc -l <span class="variable">$f</span>|awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line"><span class="keyword">for</span> l <span class="keyword">in</span> `seq 1 <span class="variable">$line</span>`; <span class="keyword">do</span></div><div class="line">     n=`sed -n <span class="string">"<span class="variable">$l</span>"</span>p <span class="variable">$f</span>|grep -o <span class="string">'[0-9]'</span>|wc -l`;</div><div class="line">     <span class="keyword">if</span> [ <span class="variable">$n</span> -eq 1 ]; <span class="keyword">then</span></div><div class="line">        sed -n <span class="string">"<span class="variable">$l</span>"</span>p <span class="variable">$f</span></div><div class="line">     <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/41. shell 练习-猜数字/">shell 练习-猜数字</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个猜数字脚本，当用户输入的数字和预设数字（随机生成一个小于100的数字）一样时，直接退出，否则让用户一直输入，并且提示用户的数字比预设数字大或者小。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">m=`<span class="built_in">echo</span> <span class="variable">$RANDOM</span>`</div><div class="line">n1=$[<span class="variable">$m</span>%100]</div><div class="line"><span class="keyword">while</span> :</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">read</span> -p <span class="string">"Please input a number: "</span> n</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$n</span> == <span class="variable">$n1</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">break</span></div><div class="line">    <span class="keyword">elif</span> [ <span class="variable">$n</span> -gt <span class="variable">$n1</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"bigger"</span></div><div class="line">        <span class="built_in">continue</span></div><div class="line">    <span class="keyword">else</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"smaller"</span></div><div class="line">        <span class="built_in">continue</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"You are right."</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/42. shell 练习-抽签脚本/">shell 练习-抽签脚本</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<ol>
<li>写一个脚本执行后，输入名字，产生随机数01-99之间的数字。</li>
<li>如果相同的名字重复输入，抓到的数字还是第一次抓取的结果，</li>
<li>前面已经抓到的数字，下次不能在出现相同数字。</li>
<li>第一个输入名字后，屏幕输出信息，并将名字和数字记录到文件里，程序不能退出</li>
</ol>
<p>继续等待别的学生输入。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">while</span> :</div><div class="line"><span class="keyword">do</span></div><div class="line"><span class="built_in">read</span> -p  <span class="string">"Please input a name:"</span> name</div><div class="line">  <span class="keyword">if</span> [ -f /work/<span class="built_in">test</span>/1.log ];<span class="keyword">then</span></div><div class="line">     bb=`cat /work/<span class="built_in">test</span>/1.log | awk -F: <span class="string">'&#123;print $1&#125;'</span> | grep <span class="string">"<span class="variable">$name</span>"</span>`</div><div class="line">     <span class="keyword">if</span> [ <span class="string">"<span class="variable">$bb</span>"</span> != <span class="string">"<span class="variable">$name</span>"</span> ];<span class="keyword">then</span>  <span class="comment">#名字不重复情况下</span></div><div class="line">        aa=`<span class="built_in">echo</span> <span class="variable">$RANDOM</span> | awk -F <span class="string">""</span> <span class="string">'&#123;print $2 $3&#125;'</span>`</div><div class="line">         <span class="keyword">while</span> :</div><div class="line">          <span class="keyword">do</span></div><div class="line">       dd=`cat  /work/<span class="built_in">test</span>/1.log |  awk -F: <span class="string">'&#123;print $2&#125;'</span>  | grep <span class="string">"<span class="variable">$aa</span>"</span>`</div><div class="line">          <span class="keyword">if</span> [ <span class="string">"<span class="variable">$aa</span>"</span>  ==  <span class="string">"<span class="variable">$dd</span>"</span> ];<span class="keyword">then</span>   <span class="comment">#数字已经存在情况下</span></div><div class="line">            <span class="built_in">echo</span> <span class="string">"数字已存在."</span></div><div class="line">            aa=`<span class="built_in">echo</span> <span class="variable">$RANDOM</span> | awk -F <span class="string">""</span> <span class="string">'&#123;print $2 $3&#125;'</span>`</div><div class="line">           <span class="keyword">else</span></div><div class="line">            <span class="built_in">break</span></div><div class="line">          <span class="keyword">fi</span></div><div class="line">          <span class="keyword">done</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"<span class="variable">$name</span>:<span class="variable">$aa</span>"</span> | tee -a /work/<span class="built_in">test</span>/1.log</div><div class="line">     <span class="keyword">else</span></div><div class="line">     aa=`cat /work/<span class="built_in">test</span>/1.log |  grep <span class="string">"<span class="variable">$name</span>"</span> | awk -F: <span class="string">'&#123;print $2&#125;'</span>` <span class="comment">#名字重复</span></div><div class="line">       <span class="built_in">echo</span> <span class="variable">$aa</span></div><div class="line">       <span class="built_in">echo</span> <span class="string">"重复名字."</span></div><div class="line">     <span class="keyword">fi</span></div><div class="line">  <span class="keyword">else</span></div><div class="line">      aa=`<span class="built_in">echo</span> <span class="variable">$RANDOM</span> | awk -F <span class="string">""</span> <span class="string">'&#123;print $2 $3&#125;'</span>`</div><div class="line">      <span class="built_in">echo</span> <span class="string">"<span class="variable">$name</span>:<span class="variable">$aa</span>"</span> | tee -a  /work/<span class="built_in">test</span>/1.log</div><div class="line">  <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>





	<span class="different-posts">📖 <a href="/page/10">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
